
// ing
export const uploadFile = () => {
	return new Promise((resolve, reject) => {
		let imgList = [];
		uni.chooseImage({
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				const uploadPromises = tempFilePaths.map((item) => {
					return new Promise((resolveImage, rejectImage) => {
						uni.compressImage({
							src: item,
							quality: 10,
							success: (compressRes) => {
								console.log(compressRes
									.tempFilePath);
								const compressFile = compressRes
									.tempFilePath;
								uni.uploadFile({
									url: 'http://localhost:8080/apis/uploadFiles', // 仅为示例，非真实的接口地址
									filePath: compressFile,
									name: 'file',
									header: {
										'Authorization': 'Bearer ' +
											uni
											.getStorageSync(
												'jyu_token'
												) || useUser
											.token || ''
									},
									success: (
										uploadFileRes) => {
											const
												fileUrlTemp =
												'http://localhost:8080' +
												JSON.parse(
													uploadFileRes
													.data)
												.filePaths[
													0];
											imgList.push(
												fileUrlTemp
												);
											resolveImage();
										},
									fail: (uploadErr) => {
										rejectImage(
											uploadErr
											);
									}
								});
							},
							fail: (compressErr) => {
								rejectImage(compressErr);
							}
						});
					});
				});

				Promise.all(uploadPromises)
					.then(() => {
						resolve(imgList); 
					})
					.catch((error) => {
						reject(error);
					});
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};